package com.mxpio.erp.asset.controller;

import com.mxpio.erp.common.asset.entity.BorrowApply;
import com.mxpio.erp.common.asset.entity.BorrowOutMaterialFlow;
import com.mxpio.erp.common.asset.service.BorrowApplyService;
import com.mxpio.erp.common.asset.service.BorrowOutMaterialFlowService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.service.ItemService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : 吴盼盼
 * @Datetime : 2022/9/15 10:16
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Tag(name = "BorrowOutMaterialFlowController", description = "资产借用归还流水接口")
@RestController
@RequestMapping("/erp/asset/borrowoutmaterialflow/")
public class BorrowOutMaterialFlowController {
	@Autowired
	private BorrowOutMaterialFlowService borrowOutMaterialFlowService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private BorrowApplyService borrowApplyService;
    @GetMapping("page")
    @Operation(summary = "资产借用行已借出表", description = "资产借用行已借出表（分页）", method = "GET")
    public Result<Page<BorrowOutMaterialFlow>> page(@Parameter(name="查询参数")  Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BorrowOutMaterialFlow> flowPage = borrowOutMaterialFlowService.listPage(BorrowOutMaterialFlow.class, page, criteria);
        List<BorrowOutMaterialFlow> content = flowPage.getContent();
        if (content.size()>0){
            Map<String, Item> itemMap = new HashMap<>();
            Map<String, BorrowApply> applyMap = new HashMap<>();
            for (BorrowOutMaterialFlow flow : content){
                flow.setOutFlowLotReturnedSum(borrowOutMaterialFlowService.getSingleOutFlowLotReturnedSum(flow.getOriginBizNo(),flow.getOriginBizLineNo(),flow.getLotNo()));//关联借出行 其中批次已归还数量
                Item item = null;
                if (itemMap.containsKey(flow.getItemCode())){
                    item = itemMap.get(flow.getItemCode());
                }else{
                    item = itemService.getById(Item.class,flow.getItemCode());
                    itemMap.put(flow.getItemCode(),item);
                }
                BorrowApply apply = null;
                if (applyMap.containsKey(flow.getItemCode())){
                    apply = applyMap.get(flow.getItemCode());
                }else{
                    apply = borrowApplyService.getById(BorrowApply.class,flow.getOriginBizNo());
                    applyMap.put(flow.getItemCode(),apply);
                }
                if (item!=null){
                    flow.setItemName(item.getItemName());
                    flow.setItemSpec(item.getItemSpec());
                    flow.setUnitCode(item.getUnitCode());
                    flow.setImgFile(item.getImgFile());
                }
                if (apply!=null){
                    flow.setApplyDept(apply.getApplyDept());
                    flow.setBorrower(apply.getBorrower());
                    flow.setPlanReturnTime(apply.getPlanReturnTime());
                }
            }

        }
        return Result.OK(flowPage);
    }

}
